<?php
//模型
class MemberPhysicalModel extends MY_Model {
	function __construct(){
		parent::__construct();
		$this->table = 'user_physical';
	}
	
	function get_data($user_id,$page=1){
		
		$where = " user_id='{$user_id}' ";
		
		$sql  = "select date from ".$this->db->dbprefix.$this->table." where {$where} group by `date` order by date desc limit ".($page-1).",1";
		
		$query = $this->db->query($sql);
		$ls_row = $query->row_array();	
		
		if($ls_row){
			$date = $ls_row['date'];
		}
		else{
			$date = date('Y-m-d');
		}

		
		$sql = "select * from ".$this->db->dbprefix."user_physical_type ";
		$query = $this->db->query($sql);
		$res = $query->result_array();
		
		$ids = array();
		$data = array();
		foreach($res as $val){
			
			$sql  = "select record from ".$this->db->dbprefix.$this->table." where user_id='{$user_id}' AND type_id ='{$val['id']}' AND `date`='{$date}' limit 1";
			$query = $this->db->query($sql);
			$row = $query->row_array();

			
			$data[$val['code']] = isset($row['record'])?$row['record']:'';
		}
		
		$sql = "select count(*) as num from (select count(id) from ".$this->db->dbprefix.$this->table." where {$where} group by `date`) as u";
		$query = $this->db->query($sql);
		$ls_row = $query->row_array();
		$count = $ls_row['num'];
		
		return array('data'=>$data,'count'=>$count,'date'=>$date);
	}
	

}

